package hihocode;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class SearchKnight {

	private static final int[][] dir = {{-1, 2}, {1, 2}, {-1, -2}, {1, -2}, 
		{-2, 1}, {2, 1}, {-2, -1}, {2, -1}};

	private final int rows = 8, columns = 8;
	
	private int max = (int)Math.pow(8, 6) + 2;
	
	private boolean[] visited = new boolean[max];
	
	private int startX1, startY1;
	private int startX2, startY2;
	private int startX3, startY3;
	
	private int n;
	private long res;
	
	public static void main(String[] args) {
		SearchKnight object = new SearchKnight();
		object.getRes();
	}
	
	public void getRes(){
		Scanner scanner = new Scanner(System.in);
		int testCase = scanner.nextInt();
		for(int t = 1; t <= testCase; t++){
			res = 0;
			String start1 = scanner.next();
			String start2 = scanner.next();
			String start3 = scanner.next();
			int[] first = new int[6];
			first[1] = start1.charAt(0) - 'A';
			first[0] = start1.charAt(1) - '1';
			first[3] = start2.charAt(0) - 'A';
			first[2] = start2.charAt(1) - '1';
			first[5] = start3.charAt(0) - 'A';
			first[4] = start3.charAt(1) - '1';
			bfs(first);
			System.out.println(res);
		}
		scanner.close();
	}
	
	private class Node{
		//我们使用一个6位数的八进制来代表每个骑士
		//比如"B2 D3 F4"，就表示为"113253"
		int[] cur;
		int step;
		public Node(int[] cur){
			this.cur = cur;
		}
	}
	
	public void bfs(int[] first){
		Queue<Node> queue = new LinkedList<>();
		Arrays.fill(visited, false);
		Node node = new Node(first);
		queue.add(node);
		while(!queue.isEmpty()){
			node = queue.poll();
			//如果三位骑士的位置一样了
			if( (node.cur[0] == node.cur[2] && node.cur[1] == node.cur[3]) && 
					(node.cur[0] == node.cur[4] && node.cur[1] == node.cur[5])){
				res = node.step;
				return;
			}
//			if( (node.cur & 0x3f) == ((node.cur >>> 6) & 0x3f) && 
//					(node.cur & 0x3f) == ((node.cur >>> 12) & 0x3f)){
//				res = node.step;
//			}
			//枚举不同的棋子
			for(int i = 0; i < 3; i++){
				for(int j = 0; j < dir.length; j++){
					int[] next = move(node.cur, i, j);
					int cur = getValue(next);
					if(isValid(next) && !visited[cur]){
						visited[cur] = true;
						Node nextNode = new Node(next);
						nextNode.step = node.step + 1;
						queue.add(nextNode);
					}
				}
			}
		}
	}

	private int getValue(int[] next) {
		int res = 0;
		int base = 1;
		for(int i = 0; i < next.length; i++){
			res += next[i] * base;
			base *= 8;
		}
		return res;
	}

	private int[] move(int[] cur, int i, int j) {
		int[] res = Arrays.copyOf(cur, cur.length);
		res[i * 2] += dir[j][0];
		res[i * 2 + 1] += dir[j][1];
		return res;
	}
	public boolean isValid(int[] next){
		for(int i = 0; i < 6; i++){
			if(next[i]< 0 || next[i] >= rows){
				return false;
			}
		}
		return true;
	}
}
